package com.hp.gxw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hp.gxw.cache.CommonCache;
import com.hp.gxw.constant.BaseEntry;
import com.hp.gxw.constant.LayTableR;
import com.hp.gxw.constant.R;
import com.hp.gxw.entity.BaseCommonDataDictionaryAndSet;
import com.hp.gxw.entity.CostEquipment;
import com.hp.gxw.entity.CostEquipmentDepreciation;
import com.hp.gxw.service.ICostEquipmentDepreciationService;
import com.hp.gxw.service.ICostEquipmentService;
import com.hp.gxw.utils.ConditionUtil;
import com.hp.gxw.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangping
 * @since 2024-01-04
 */
@Controller
@RequestMapping("/manage/equipment_depreciation")
public class CostEquipmentDepreciationController extends  CommonController{

    @Autowired
    private ICostEquipmentService iCostEquipmentService;

    @Autowired
    private ICostEquipmentDepreciationService iCostEquipmentDepreciationService;

    @Autowired
    public CommonCache commonCache;

    @PostMapping("/list/{happenMonth}")
    public LayTableR list(@PathVariable("happenMonth") String happenMonth, Integer page, Integer limit, String master) throws Exception {
        /**初始化发生月数据*/
        List<CostEquipment> elementsList = iCostEquipmentService.depreciationListInfo(happenMonth);
        List<CostEquipmentDepreciation> list = new ArrayList<>();
        for (CostEquipment costEquipment : elementsList) {
            if(costEquipment.getMonthDepreciationAmount() == null){
                CostEquipmentDepreciation costEquipmentDepreciation = new CostEquipmentDepreciation();
                costEquipmentDepreciation.setEquipmentId(costEquipment.getId());
                costEquipmentDepreciation.setDepreciationAmount(costEquipment.getDepreciationAmount()==null?BigDecimal.ZERO:costEquipment.getDepreciationAmount().divide(costEquipment.getDepreciationYear().multiply(new BigDecimal(12)),2,BigDecimal.ROUND_HALF_UP));
                costEquipmentDepreciation.setDisable(false);
                costEquipmentDepreciation.setHappenMonth(happenMonth);
                costEquipmentDepreciation.setCreateUser(ShiroUtil.getUserId());
                costEquipmentDepreciation.setCreateTime(new Date());
                list.add(costEquipmentDepreciation);
            }
        }
        if(list.size() > 0){
            iCostEquipmentDepreciationService.saveBatch(list);
        }

        QueryWrapper<Object> ew = ConditionUtil.convert(master, new QueryWrapper<>());
        ew.eq("happen_month", happenMonth);
        BaseEntry baseEntry = commonCache.init("cost_equipment_depreciation");
        IService<Object> iService = baseEntry.getIService();
        Page<Object> pageList = iService.page(new Page<>(page, limit), ew);
        List<BaseCommonDataDictionaryAndSet> collect = baseEntry.getSetList().stream().filter(e -> e.getQueryConstantId() != null).collect(Collectors.toList());
        List<Map> resultList = dataTransfer(pageList.getRecords(), collect);
        return LayTableR.result(0, pageList.getTotal(), resultList);
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody List<CostEquipmentDepreciation> list) {
        try {
            for (CostEquipmentDepreciation costEquipmentDepreciation : list) {
                costEquipmentDepreciation.setCreateUser(ShiroUtil.getUserId());
                costEquipmentDepreciation.setCreateTime(new Date());
            }
            iCostEquipmentDepreciationService.updateBatchById(list);
        } catch (Exception e) {
            return R.error("系统异常");
        }
        return R.ok();
    }
}

